﻿using System;
using System.Collections.Generic;
using System.Text;

namespace FunctionalCLR.Core {
    public class DefaultExpressionCompiler : IExpressionCompiler {
        #region IExpressionCompiler Members

        public string Compile(FunctionalCLR.Core.Node node) {
            string b = string.Empty;
            if (node.GetType().IsSubclassOf(typeof(OperatorNode))) {
                OperatorNode fn = (OperatorNode)node;
                b = "(";
                b += this.Compile(fn.Left);
                b += fn.Value;
                b += this.Compile(fn.Right);
                b += ")";
            } else if (node.GetType().IsSubclassOf(typeof(FuncNode))) {
                FuncNode fn = (FuncNode)node;
                b += fn.Value + "(";
                for(int index = fn.Children.Count - 1; index >= 0; index--) {
                    b += this.Compile(fn.Children[index]);
                    if (index > 0) {
                        b += ",";
                    }
                }
                b += ")";
            } else {
                b = node.Value;
            }

            return b;
        }

        #endregion
    }
}
